package main

import (
	"fmt"
	"sync"
	"time"
)

// 声明全局计数器锁
var wg sync.WaitGroup

func printOdd() {
	defer wg.Done() // 结束执行后计数器-1
	var num = 1
	for num <= 5 {
		fmt.Println(num)
		time.Sleep(time.Second * 1)
		num += 2
	}
}

func printEven() {
	defer wg.Done() // 结束执行后计数器-1
	var num = 0
	for num <= 5 {
		fmt.Println(num)
		time.Sleep(time.Second * 1)
		num += 2
	}
}

func main() {
	// 计数器+2
	wg.Add(2)
	// 启动分线程1
	go printOdd()
	// 启动分线程2
	go printEven()
	// 阻塞等待计数器变为0
	wg.Wait()
}
